using UnityEngine;
using System.Collections;

public class Utils {

    //Place Anchors with camera parameters
    public static void PlaceHUDAnchors(Camera cam, Transform parent, out GameObject leftUp, out GameObject leftDown, out GameObject rightUp, out GameObject rightDown)
    {
        GameObject planeAnchor = GameObject.CreatePrimitive(PrimitiveType.Plane);
        planeAnchor.transform.localScale = new Vector3(50, 50, 50);
        planeAnchor.transform.position = new Vector3(cam.transform.position.x, cam.transform.position.y, 50);
        planeAnchor.transform.Rotate(new Vector3(1, 0, 0), -90);

        leftUp = new GameObject();
        leftDown = new GameObject();
        rightUp = new GameObject();
        rightDown = new GameObject();

        //Left Down
        Ray ray = cam.ScreenPointToRay(new Vector2(0, 0));
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 500))
        {
            leftDown.name = "Anchor Left Down";
            leftDown.transform.position = hit.point;
        }

        //Left Up
        ray = cam.ScreenPointToRay(new Vector2(0, Screen.height));
        if (Physics.Raycast(ray, out hit, 500))
        {
            leftUp.name = "Anchor Left Up";
            leftUp.transform.position = hit.point;
        }

        //Right Down
        ray = cam.ScreenPointToRay(new Vector2(Screen.width, 0));
        if (Physics.Raycast(ray, out hit, 500))
        {
            rightDown.name = "Anchor Right Down";
            rightDown.transform.position = hit.point;
        }

        //Right Up
        ray = cam.ScreenPointToRay(new Vector2(Screen.width, Screen.height));
        if (Physics.Raycast(ray, out hit, 500))
        {
            rightUp.name = "Anchor Right Up";
            rightUp.transform.position = hit.point;
        }

        GameObject.Destroy(planeAnchor);

        leftDown.transform.parent = parent;
        leftUp.transform.parent = parent;
        rightDown.transform.parent = parent;
        rightUp.transform.parent = parent;

    }



}
